package com.warmlight.voicepacket.ui.user;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.MyMessageListAdapter;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpUtils;
import com.warmlight.voicepacket.http.ResponseData;

import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 我的消息  废弃
 */
public class MyMessageListActivity extends BaseActivity {


    private SmartRefreshLayout refreshLayout;
    private RecyclerView rc_msg;
    private int page = 0;
    private MyMessageListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mymessage_list);
        initView();
        initListener();
        initData();
        refreshLayout.autoRefresh();
    }

    private void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        rc_msg.setLayoutManager(manager);
        adapter = new MyMessageListAdapter(getContext());
        rc_msg.setAdapter(adapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData(true);
            }
        });
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

    }

    private void loadData(boolean isRefresh){
        if (isRefresh){
            page = 1;
        }else{
            page ++;
        }
        String url = "";
        TreeMap<String,String> treeMap = new TreeMap<>();
        HttpClient.getInstance().postRxAndroidAES(url, treeMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (HttpUtils.isCodeSuccess(responseData.getCode())){

                        }else{

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initView() {
        setBack();
        setTitle("消息");
        refreshLayout = findViewById(R.id.refreshLayout);
        rc_msg = findViewById(R.id.rc_msg);
    }
}
